package icu.shaoyayu.common.web.swagger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author shaoyayu
 * @date 2021/12/25
 * @apiNote
 */
@EnableSwagger2
@Configuration
@ConditionalOnMissingBean(Swagger2AutoConfigure.class)
@EnableConfigurationProperties(Swagger2ApiInfoProperties.class)
public class Swagger2AutoConfigure {


    @Autowired
    private Swagger2ApiInfoProperties swagger2ApiInfoProperties;

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(swagger2ApiInfoProperties.getScanPackagePath()))
                // 可以根据url路径设置哪些请求加入文档，忽略哪些请求
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //设置文档的标题
                .title(swagger2ApiInfoProperties.getTitle())
                // 设置文档的描述
                .description(swagger2ApiInfoProperties.getDescription())
                // 设置文档的版本信息-> 1.0.0 Version information
                .version(swagger2ApiInfoProperties.getVersion())
                // 设置文档的License信息->1.3 License information
                .termsOfServiceUrl(swagger2ApiInfoProperties.getUrl())
                .build();
    }

}
